package com.zjut.zs.zxlb.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zjut.an.log.dao.AnLog;
import com.zjut.an.log.manager.AnLogManager;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.system.Status;
import com.zjut.an.util.tools.DateUtil;
import com.zjut.an.util.tools.FileDownload;
import com.zjut.an.util.tools.JSONUtilClass;
import com.zjut.an.util.tools.JSONUtilClassTime;
import com.zjut.an.util.tools.StringUtil;
import com.zjut.common.idgen.IdGenerate;
import com.zjut.zs.file.dao.ZsFile;
import com.zjut.zs.file.manager.ZsFileManager;
import com.zjut.zs.hjqk.dao.ZsHjqk;
import com.zjut.zs.user.dao.ZsUser;
import com.zjut.zs.user.manager.ZsUserManager;
import com.zjut.zs.userstatus.dao.ZsUserStatus;
import com.zjut.zs.userstatus.manager.ZsUserStatusManager;
import com.zjut.zs.zxlb.dao.ZsZxlb;
import com.zjut.zs.zxlb.manager.ZsZxlbManager;

public class ZsZxlbAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, ModelDriven<ZsZxlb>{

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	
	private ZsZxlb obj;
	private ZsFile file;
	private Map<String , Object> dataMap;
	private int offset = 0, limit = 10;
	private String order;
	private String sort;
	private String search;
	private String rowstr;
	
	@Resource(name = "ZsZxlbManager")
	private ZsZxlbManager manager;

	@Resource(name = "ZsUserManager")
	private ZsUserManager umanager;
	
	@Resource(name = "FileDownload")
	private FileDownload filedownload;
	
	@Resource(name = "ZsFileManager")
	private ZsFileManager fmanager;

	@Resource(name = "AnLogManager")
	private AnLogManager logmanager;

	@Resource(name = "DateUtil")
	private DateUtil dateutil;
	
	@Resource(name = "ZsUserStatusManager")
	private ZsUserStatusManager smanager;

	@Resource(name = "IdGenerate")
	private IdGenerate idgenerate;
	
	@Resource(name = "StringUtil")
	private StringUtil stringutil;
	
	public String zsZxlbList() {
		try {
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();
			int pageNumber = offset/limit + 1;
			PageModule module = manager.getDataByInstance(pageNumber, limit, sort, order, obj);
			dataMap.put("rows", module.getObjects());
			dataMap.put("total", module.getTotal()); 
			obj = null; 
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	@SuppressWarnings("unchecked")
	public String zsZxlbDeletes() {
		try {
			String objumniJSON = rowstr;
			manager.deleteDatas(JSONUtilClass.getDTOList(objumniJSON, ZsZxlb.class));				
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsZxlbDelete() {
		try {
			String objumniJSON = rowstr;			
			//manager.deleteData((ZsZxlb)JSONUtilClass.getDTO(objumniJSON, ZsZxlb.class));
			obj = manager.getData(request.getParameter("zxId"));
			AnLog log = new AnLog("报考日志",logmanager.subIdcard(((ZsUser)session.getAttribute("zsUser")).getUsIdcard())+"于"+dateutil.getCurrentDate5()+"删除专项"+obj.getZxName(), dateutil.getCurrentDate5());
			logmanager.addData(log);
			
			ZsFile file = new ZsFile();
			file.setFiName(obj.getZxId() + "");
			file.setFiType("zxlb");
			file.setZsUser(obj.getZsUser());
			List flist = new ArrayList();
			flist = fmanager.findByExample(file);			
			manager.deleteData(obj);
			fmanager.deleteDatas(flist);
			smanager.updateFilesColumn(obj.getZsUser(), Status.ZS_ZXLB);
			smanager.updatefileStatus(obj.getZsUser());//修改材料上传状态	
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsZxlbUpdate() {
		try {
			String objumniJSON = rowstr;
			manager.updateData((ZsZxlb)JSONUtilClass.getDTO(objumniJSON, ZsZxlb.class));
			smanager.updateFilesColumn(obj.getZsUser(), Status.ZS_ZXLB);
			//filedownload.downloadFile(obj.getZxFiles(), umanager.getData(obj.getZsUser().getUsId()).getUsIdcard());
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsZxlbSave() {
		System.out.println(rowstr);
		obj = (ZsZxlb)(JSONUtilClass.getDTO(rowstr, ZsZxlb.class));
		System.out.println(obj.getZxId());
		/*
		 * String filenames = obj.getZxFiles(); String[] Str1Array =
		 * filenames.split(";"); obj.setZxFiles("");
		 * file = new ZsFile(); file.setFiType("zxlb"); file.setZsUser(obj.getZsUser());
		 */
		if (!stringutil.isEmpty(obj.getZxId())) {
			//-------update-------
			manager.updateData(obj);
		} else {
			//-------insert-------
			obj.setZxId(idgenerate.nextId());
			manager.addData(obj);
		}
		AnLog log = new AnLog("报考日志",logmanager.subIdcard(((ZsUser)session.getAttribute("zsUser")).getUsIdcard())+"于"+dateutil.getCurrentDate5()+"修改专项"+obj.getZxName(), dateutil.getCurrentDate5());
		logmanager.addData(log);
		/*
		 * obj = (ZsZxlb) manager.getDataByInstance(1, 1, null, null, obj)
		 * .getObjects().get(0); file.setFiName(obj.getZxId() + ""); if
		 * (!filenames.equals("")) { for (int i = 0; i < Str1Array.length; i++) {
		 * System.out.println(Str1Array[i]); filedownload.downloadFile(Str1Array[i],
		 * umanager.getData(obj.getZsUser().getUsId()) .getUsIdcard());
		 * file.setFiFile(Str1Array[i]); List flist = new ArrayList(); flist =
		 * fmanager.findByExample(file); if (flist.size() == 0) fmanager.addData(file);
		 * } }
		 */
		smanager.updateFilesColumn(obj.getZsUser(), Status.ZS_ZXLB);
		smanager.updatefileStatus(obj.getZsUser());//修改材料上传状态		
		rowstr = null;
		return SUCCESS;
	}

	public String zsZxlb() {
		try {
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();
			String inId = request.getParameter("inId");
			obj = manager.getData(inId);
			dataMap.put("rows", obj);
			obj = null;
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	@Override
	public ZsZxlb getModel() {
		if (null == obj) {
			obj = new ZsZxlb();
		}
		return obj;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public ZsZxlb getAl() {
		return obj;
	}

	public void setAl(ZsZxlb obj) {
		this.obj = obj;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getRowstr() {
		return rowstr;
	}

	public void setRowstr(String rowstr) {
		this.rowstr = rowstr;
	}

	public ZsZxlbManager getManager() {
		return manager;
	}

	public void setManager(ZsZxlbManager manager) {
		this.manager = manager;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

}
